<?php

class CommentController extends Controller_Site
{
	public function indexAction()
	{
		$this->_forward('add');
	}

	public function ajaxCommentsAction()
	{
		$this->_ajaxOnly();

		$substanceId = $this->_getParam('substanceId');
		$substanceTypeId = $this->_getParam('substanceTypeId');

		$this->_initComments($substanceId, $substanceTypeId);
	}

	/**
	 * Adding comment by Request params.
	 *
	 * @return void
	 */
	public function addAction()
	{
		$this->_ajaxOnly();

		// Define input filters
		$filters = array(
			'content' => array(
				new Zend_Filter_StringTrim(),
			),
		);

		// Retrieve substance type row for get table where check commented content for existing.
		$substanceTypeId = $this->_getParam('substanceTypeId', 0);
		$substanceTypeRow = CommentSubstanceTypePeer::getInstance()->find($substanceTypeId)->current();
		$table = 'comment';
		if ($substanceTypeRow) {
			$table = $substanceTypeRow->getTable();
			
			// check if we need to log this action
			if ($table == 'photo' || $table == 'video') {
				$needToLog = true;
			}
		}

		// Define input validators
		$validators = array(
			'content' => array(
				new Validate_Authenticated(),
				new Zend_Validate_NotEmpty(),
				new Zend_Validate_StringLength(0, 1000),
				Zend_Filter_Input::MESSAGES => array(
					//Zend_Validate_NotEmpty::IS_EMPTY => 'Вы забыли ввести комментарий перед отправкой',
					//Zend_Validate_StringLength::TOO_LONG  => "Максимальная длина комментария ограничена 1000 символами",
				),
			),
			'substanceTypeId' => array(
				new Zend_Validate_NotEmpty(),
				new Zend_Validate_Digits(),
				new Zend_Validate_Db_RecordExists('comment_substance_type', 'id'),
			),
			'substanceId' => array(
				new Zend_Validate_NotEmpty(),
				new Zend_Validate_Digits(),
				new Zend_Validate_Db_RecordExists($table, 'id'),
			),
			'parentId' => array(
				new Zend_Validate_Digits(),
				//new Zend_Validate_Db_RecordExists('comment', 'id'),
				Zend_Filter_Input::ALLOW_EMPTY => true,
			),
		);

		// Retrieve request data
		$data = $this->_getAllParams();

		// Check data for valid
		$input = new Zend_Filter_Input($filters, $validators, $data);
		if (!$input->isValid()) {
			$messages = $input->getMessages();
			$this->view->commentError = current(current($messages));
			$this->_initComments($this->_getParam('substanceId'), $this->_getParam('substanceTypeId'));
			return ;
		}

		// Save needed for set data
		$substanceTypeId = $input->getEscaped('substanceTypeId');
		$substanceId = $input->getEscaped('substanceId');
		//$parentId = $input->getEscaped('parentId');
		$content = $input->getUnescaped('content');
		$profileId = $this->_getProfile()->getId();

		// Creating new comment
		$row = CommentPeer::getInstance()->createRow();
		$row->setSubstanceTypeId($substanceTypeId);
		$row->setSubstanceId($substanceId);
		$row->setProfileId($profileId);
		$row->setText($content);
		
		$row->save();
//		if ($needToLog) {
//			// adding row to user action log
//			$actionRow = ProfileActionsLogPeer::getInstance()->createRow();
//			$actionRow->setProfileId($profileId);
//			$actionTypeRow = ProfileActionTypePeer::getInstance()->fetchByKey('comment');
//			$actionRow->setActionTypeId($actionTypeRow->getId());
//			$actionRow->setActionId($row->getId());
//			$actionRow->save();			
//		}

		// Adding to view comments
		//$this->_initComments($substanceId, $substanceTypeId);
	}


	/**
	 * Just delete comment. Check by profile_id equal Zend_Auth identity.
	 *
	 * @return void
	 */
	public function deleteAction()
	{
		$this->_ajaxOnly();

		$commentRow = CommentPeer::getInstance()->find($this->_getParam('id', 0))->current();
		if (!$commentRow instanceof CommentRow) {
			throw new Controller_InvalidArgumentException('Comment not found');
		}

		// Commented object Row.
		$substanceRow = $commentRow->getSubstanceRow();

		// Validate comment owner.
		$exclude = null;
		$profile = $this->_getProfile();
		if ($profile) {
			$id = $profile->getId();
			$exclude = "`profile_id` = $id";
		}

		$validatorIsOwner = new Zend_Validate_Db_RecordExists('comment', 'id', $exclude);
		$validatorIsOwner->setMessage('Нельзя удалить чужой комментарий', Zend_Validate_Db_Abstract::ERROR_NO_RECORD_FOUND);
		$profile = $this->_getProfile();

		// Define validators
		$validators = array(
			'id' => array(
				new Validate_Authenticated(),
				$validatorIsOwner,
			),
		);

		// Retrieve request data
		$data = $this->_getAllParams();

			// Check data for valid
//		$input = new Zend_Filter_Input(array(), $validators, $data);
//		if (!$input->isValid()) {
//			$messages = $input->getMessages();
//			$this->view->commentError = current(current($messages));
//			$this->_initComments($substanceRow->getId(), $substanceRow->getTable());
//			//$this->view->comments = $substanceRow->getComments();
//			return ;
//		}


		$commentRow->delete();
		//$this->_initComments($substanceRow->getId(), $substanceRow->getTable()->getTableName());
		
		
		//$this->view->comments = $comments;  
		
	}


}